thrift = require 'thrift'
user = require __basename + '/helpers/gen-nodejs/AccountService'
usertypes = require __basename + '/helpers/gen-nodejs/AccountCenter_types'
server = require __basename + '/connect/thrift/thrift'
client = thrift.createClient user, server

module.exports =

  registerUser : (data, fn) ->
    data.sex = new usertypes.SexVO(data.sex) if data.sex
    client.registerUser new usertypes.UserVO(data), (err, results) ->
      fn err, results

  login : (email, password, fn) ->
    client.login email, password, (err, results) ->
      fn err, results

  loginByName : (name, password, fn) ->
    client.loginByName name, password, (err, results) ->
      fn err, results

  getUserByEmail : (email, fn) ->
    client.getUserByEmail email, (err, results) ->
      fn err, results

  getUserById : (userId, fn) ->
    client.getUserById userId, (err, results) ->
      fn err, results

  getUserByNickname : (nickname, fn) ->
    client.getUserByNickname nickname, (err, results) ->
      fn err, results

  getUserByMobile : (mobile, fn) ->
    client.getUserByMobile mobile, (err, results) ->
      fn err, results

  updateUserBasicInfo : (data, fn) ->
    data.sex = new usertypes.SexVO(data.sex) if data.sex
    client.updateUserBasicInfo new usertypes.UserVO(data), (err, results) ->
      fn err, results

  updateCartId : (userId, cartId, fn) ->
    client.updateCartId userId, cartId, (err, results) ->
      fn err, results

  changePassword : (data..., fn) ->
    client.changePassword data[0], data[1], data[2], (err, results) ->
      fn err, results

  resetPassword : (email, newPwd, fn) ->
    client.resetPassword email, newPwd, (err, results) ->
      fn err, results

  checkName : (nickname, fn) ->
    client.checkName nickname, (err, results) ->
      fn err, results

  checkEmail : (email, fn) ->
    client.checkEmail email, (err, results) ->
      fn err, results

  isOfflineUser : (nickname, fn) ->
    client.isOfflineUser nickname, (err, results) ->
      fn err, results

  checkVipValid : (mobile, fn) ->
    client.checkVipValid mobile, (err, results) ->
      fn err, results

  createOfflineActivationCode : (userId, mobile, fn) ->
    client.createOfflineActivationCode userId, mobile, (err, results) ->
      fn err, results

  checkOfflineActivationCode : (data ..., fn) ->
    client.checkOfflineActivationCode data[0], data[1], data[2], (err, results) ->
      fn err, results

  activateUser : (userId, mobile, fn) ->
    client.activateUser userId, mobile, (err, results) ->
      fn err, results

  createAndSendOfflineActivationCode : (userId, mobile, fn) ->
    client.createAndSendOfflineActivationCode userId, mobile, (err, results) ->
      fn err, results

  syncUser : (userId, mobile, fn) ->
    client.syncUser userId, mobile, (err, results) ->
      fn err, results

  getUsersByRecId : (recId, fn) ->
    client.getUsersByRecId recId, (err, results) ->
      fn err, results

  getUsersByUserIds : (userIds, fn) ->
    client.getUsersByUserIds userIds, (err, results) ->
      fn err, results

  createCustomer : (custormer_data, fn) ->
    custormer_data.accounts = new usertypes.AccountVO(custormer_data.accounts) if custormer_data.accounts
    client.createCustomer new usertypes.CustomerVO(custormer_data), (err, results) ->
      fn err, results

  updateCustomer : (custormer_data, fn) ->
    custormer_data.accounts = new usertypes.AccountVO(custormer_data.accounts) if custormer_data.accounts
    client.updateCustomer new usertypes.CustomerVO(custormer_data), (err, results) ->
      fn err, results

  createCustomerAccount : (custormer_data, fn) ->
    custormer_data.accounts = new usertypes.AccountVO(custormer_data.accounts) if custormer_data.accounts
    client.createCustomerAccount customerId, new usertypes.CustomerVO(custormer_data), (err, results) ->
      fn err, results

  getChildrenByParentId : (parentId, fn) ->
    client.getChildrenByParentId parentId, (err, results) ->
      fn err, results

  insertChild : (child_data, fn) ->
    child_data.childSex = new usertypes.SexVO(child_data.childSex) if child_data.childSex
    client.insertChild child_data, (err, results) ->
      fn err, results

  updateChild : (child_data, fn) ->
    child_data.childSex = new usertypes.SexVO(child_data.childSex) if child_data.childSex
    client.updateChild child_data, (err, results) ->
      fn err, results

  oauth : (name, password, fn) ->
    client.loginByName name, password, (err, results) ->
      if results.ret is 0
        client.getUserByNickname name, (err, results) ->
          fn err, results
      else
        fn error : 'login fail'